#region License
// Copyright 2008-2009 Jeremy Skinner (http://www.jeremyskinner.co.uk)
// 
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// The latest version of this file can be found at http://www.codeplex.com/FluentValidation
#endregion

namespace FluentValidation.Internal {
    using System;
    using System.Collections.Generic;
    using System.Reflection;

    /// <summary>
    /// Represents a chain of properties
    /// </summary>
    //TODO: Is it really best to use strings for this?
    public class PropertyChain {
        readonly List<string> _memberNames = new List<string>();

        public PropertyChain() {
        }

        public PropertyChain(PropertyChain parent) {
            if (parent != null) {
                _memberNames.AddRange(parent._memberNames);
            }
        }

        /// <summary>
        /// Adds a MemberInfo instance to the chain
        /// </summary>
        /// <param name="member">Member to add</param>
        public void Add(MemberInfo member) {
            _memberNames.Add(member.Name);
        }

        /// <summary>
        /// Adds a property name to the chain
        /// </summary>
        /// <param name="propertyName">Name of the property to add</param>
        public void Add(string propertyName) {
            _memberNames.Add(propertyName);
        }

        /// <summary>
        /// Adds an indexer to the property chain. For example, if the following chain has been constructed: 
        /// Parent.Child
        /// then calling AddIndexer(0) would convert this to:
        /// Parent.Child[0]
        /// </summary>
        /// <param name="indexer"></param>
        public void AddIndexer(object indexer) {
            if (_memberNames.Count == 0) {
                throw new InvalidOperationException("Could not apply an Indexer because the property chain is empty.");
            }

            string last = _memberNames[_memberNames.Count - 1];
            last += "[" + indexer + "]";

            _memberNames[_memberNames.Count - 1] = last;
        }

        public override string ToString() {
            return string.Join(".", _memberNames.ToArray());
        }

        /// <summary>
        /// Checks if the current chain is the child of another chain.
        /// For example, if chain1 were for "Parent.Child" and chain2 were for "Parent.Child.GrandChild" then
        /// chain2.IsChildChainOf(chain1) would be true.
        /// </summary>
        /// <param name="parentChain">The parent chain to compare</param>
        /// <returns>True if the current chain is the child of the other chain, otherwise false</returns>
        public bool IsChildChainOf(PropertyChain parentChain) {
            return ToString().StartsWith(parentChain.ToString());
        }
    }
}